﻿using System;
using System.Collections.Generic;
using System.Linq;
using AzureDiagMon.Common;
using AzureDiagMon.Common.Dtos;
using AzureDiagMon.Common.Helpers.Azure;
using AzureDiagMon.Common.Interfaces.Services;
using AzureDiagMon.Common.Services;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureDiagMon.Services.Azure
{
    public class AzureAccessService : Service, IAzureAccessService
    {
        #region Implementation of IAzureAccessService

        public bool ValidateSubscription(string subscriptionId, string key)
        {
            throw new NotImplementedException();
        }

        public bool ValidateStorageAccount(string name, string key)
        {
            try
            {
                var context = new AzureStorageContext(name, key);
                var tables = context.ListTables();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public IEnumerable<WadLogEntryTableEntity> FetchWadLogEntries(DateTime startTime, DateTime endTime, string deploymentId = null, string roleName = null, string roleInstanceId = null)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<WadLogEntryTableEntity> FetchWadLogEntries(StorageAccountDto storageAccount, string lastPartitionKey, string lastRowKey, int? count = null)
        {
            var storage = new AzureStorageContext(storageAccount.AccountName, storageAccount.AccessKey);
            var ctx = new WadLogEntryDataContext(storage.CloudTableClient.GetDataServiceContext(), "WADLogsTable");
            if (lastPartitionKey == null)
                lastPartitionKey = String.Empty;

            var query = ctx.GetDiagnosticEntities<WadLogEntryTableEntity>(DateTime.Today.Subtract(new TimeSpan(365, 0, 0, 0)), DateTime.Now, "DietPortal.WebRole_IN_0")
                                      .Execute();
            if (count.HasValue)
                return query.Take(count.Value)
                            .ToList();
            else
                return query.ToList();

        }

        public IEnumerable<WadLogEntryTableEntity> FetchWadLogEntries(StorageAccountDto storageAccount, long? fromParitionKey, long? toPartitionKey, int? count)
        {
            var storage = new AzureStorageContext(storageAccount.AccountName, storageAccount.AccessKey);
            var ctx = new WadLogEntryDataContext(storage.CloudTableClient.GetDataServiceContext(), "WADLogsTable");

            var query = ctx.GetDiagnosticEntities<WadLogEntryTableEntity>(fromParitionKey, toPartitionKey,
                            null,
                            null)
                                      .Execute();
            if (count.HasValue)
                return query.Take(count.Value);

            return query;

        }

        public IAsyncResult FetchWadLogEntriesAsync(StorageAccountDto storageAccount, long? fromParitionKey, long? toPartitionKey, int? count, AsyncCallback callback)
        {
            var storage = new AzureStorageContext(storageAccount.AccountName, storageAccount.AccessKey);
            var ctx = new WadLogEntryDataContext(storage.CloudTableClient.GetDataServiceContext(), "WADLogsTable");

            var query = ctx.GetDiagnosticEntities<WadLogEntryTableEntity>(fromParitionKey, toPartitionKey,
                            null,
                            null);

            return query.BeginExecuteSegmented(callback, query);
        }

        #endregion
    }
}
